/*
 * This file is part of yapk (yet another (auto) process killer) driver.
 *
 * Copyright (C) 2011 by Mateusz Wojcik <mewojcik@gmail.com>
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
#include <ntifs.h>
#include <ntdef.h>
#include "utils.h"


BOOLEAN RetrieveProcessNameByID(HANDLE ProcessId, CHAR * ProcessNameBuffer, int MaxLength) 
{ 
	NTSTATUS status 		= STATUS_ACCESS_DENIED; 	// Holds status of operations.
	PEPROCESS eProcess 		= NULL;						// Process informations.
	int processStringLen 	= 0;						// Holds process name string length.
	char * processName 		= 0;						// Holds 
	
	// [1] Get EPROCESS structure fot this process.
	status = PsLookupProcessByProcessId(ProcessId, &eProcess); 
	if((!NT_SUCCESS(status)) || (!eProcess)) 
	{ 
		return FALSE; 
	} 
	
	// [2] Get process name.
	processName 		= PsGetProcessImageFileName(eProcess);	
	processStringLen 	= strlen(processName);

	// [3] Copy it to buffer
	RtlCopyMemory(ProcessNameBuffer, processName,  (processStringLen <= MaxLength) ? processStringLen : MaxLength);
	
	// [4] Dereference eprocess structure.
	ObDereferenceObject(eProcess);
	
	return TRUE; 
} 
